{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.创建ndarray\n",
    "### 直接从其他序列对象创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array(1)  # 0维数组（标量）\n",
    "b = np.array([1, 2])  # 1维数组（向量）\n",
    "c = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])  # 2维数组（矩阵）\n",
    "d = np.array([[[1, 2, 3], [4, 5, 6]], [[4, 5, 6], [1, 2, 3]]])  # 3维数组（张量）\n",
    "# 多维数组需要用嵌套的列表创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 查看ndarray的属性\n",
    "data_array = d\n",
    "print(data_array.ndim)  #维度数\n",
    "print(data_array.shape) #形状（尺寸）\n",
    "print(data_array.dtype) #元素数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 直接创建特殊数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "a = np.zeros(10)# 全0数组\n",
    "b = np.zeros((2,10))# 创建多维数组，需要两层括号\n",
    "c = np.ones((3,2,10))# 全1数组\n",
    "\n",
    "print(\"a =\",a)\n",
    "print(\"\\nb =\",b)\n",
    "print(\"\\nc =\",c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 随机数组\n",
    "a = np.random.rand(10)# 均匀随机数\n",
    "b = np.random.randn(2,10)# 正态分布的随机数\n",
    "c = np.random.randint(0,100,10)# 随机整数\n",
    "\n",
    "print(\"a =\",a)\n",
    "print(\"\\nb =\",b)\n",
    "print(\"\\nc =\",c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.访问\n",
    "索引与切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 一维\n",
    "a = np.arange(10)\n",
    "print(a)\n",
    "print(a[9])\n",
    "print(a[5:-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 二维\n",
    "c = np.random.randint(0,100,[4,3])# 二维随机整数\n",
    "print(c)\n",
    "print(\"\\n\",c[3]) # 默认按照行索引\n",
    "print(\"\\n\",c[:,2]) # 按照列索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.数组函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.arange(10,20)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "print(arr.sum())  # 求和\n",
    "print(arr.max())  # 最大值\n",
    "print(arr.min())  # 最小值\n",
    "\n",
    "print(arr.mean())  # 平均值\n",
    "print(arr.std())  # 标准差\n",
    "print(arr.var())  # 方差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.矢量运算\n",
    "### 3.1 逐元素运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 内置数学函数：按照元素逐位运算\n",
    "print(np.square(arr),'\\n') #平方\n",
    "print(np.exp(arr),'\\n') #指数\n",
    "print(np.sin(arr),'\\n') #正弦函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 相同元素的数组运算：按照元素逐位运算\n",
    "print(arr+arr,'\\n')\n",
    "print(arr-arr,'\\n')\n",
    "print(arr*arr,'\\n')\n",
    "print(arr/arr,'\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 广播\n",
    "不同形状的数组之间的运算方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "arr1 = np.arange(7)\n",
    "print(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 标量10广播到了其他元素上\n",
    "arr2 = arr1*10\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 矩阵与向量运算\n",
    "arr1 = np.arange(4).reshape(-1,1)\n",
    "arr1 = np.tile(arr1,3)\n",
    "arr2 = np.arange(4)\n",
    "\n",
    "print(arr1)\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 矩阵的广播\n",
    "arr1 = np.arange(7)\n",
    "arr2 = np.arange(1,4)\n",
    "print(arr1)\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1+arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 线性代数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "arr1 = np.arange(1,9).reshape(2,4)\n",
    "arr2 = np.arange(1,13).reshape(4,3)\n",
    "print(arr1)\n",
    "print(arr2)\n",
    "\n",
    "arr1.dot(arr2) # 矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1.T #矩阵转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1596626384806",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}